<template>
  <el-dropdown trigger="hover" @command="changeLanguage">
    <div>
      <svg-icon style="color: #fff; font-size: 20px" icon-class="language" />
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item command="zh" :disabled="'zh' === $i18n.locale">
        中文
      </el-dropdown-item>
      <el-dropdown-item command="en" :disabled="'en' === $i18n.locale">
        en
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import cookie from 'js-cookie'
export default {
  name: 'Lang',
  methods: {
    // 点击可以获取对应菜单项上绑定的command="zh"的值
    changeLanguage (lang) {
      // 给this.$i18n.locale重新赋值
      this.$i18n.locale = lang // 设置给本地的i18n插件
      // 语法 cookie.set(key,value,{expires:day/date对象})
      // 本地持久化存储语标识=>默认存的是session形式=>关闭浏览器数据就会被删除
      cookie.set('hr-lang', lang)
      // cookie.set('test-hr', 123, { expires: 3 })

      this.$message.success(`切换${{ lang }}语言成功`)
    }
  }
}
</script>
